<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="shopcar"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="标题" prop="spuTitle" width="120">
      </el-table-column>
      <el-table-column label="图片" width="120">
        <template slot-scope="scope">
          <img
            style="width: 80px; height: 80px"
            :src="scope.row.images"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.store_nums"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
          <el-button type="primary" @click="del(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    全选<input type="checkbox" v-model="checked" @change="checkall" /> 总价{{
      allPrice
    }}
    <!-- 全选按钮 -->
    <el-button type="primary" @click="removeAll">选中删除</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checked: false,
      selectValue: [],
    };
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val);
      // if(val.length==this.$store.state.shopcar.length){
      //   this.checked=true
      // }else{
      // this.checked=false
      // }
      // 可以写成
      this.selectValue = val;
      //把选中的存下来

      this.checked = val.length == this.$store.state.shopcar.length;
    },
    //element-iu表格自带的复选框选中的事件 并且能拿到选中的数据
    checkall() {
      if (this.checked == true) {
        this.$refs.multipleTable.toggleAllSelection();
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    //下面的全选按钮控制 复选框 全部选中 或者 全不选中 这里也是借助element-ui自带的方法
    removeAll() {
      this.$store.commit("removeAll", this.selectValue);
    },
    //选中删除
    del(index) {
      this.$store.commit("del", index);
    },
  },
  created() {},
  mounted() {},
  components: {},
  computed: {
    shopcar() {
      return this.$store.state.shopcar;
    },
    allPrice() {
      let p = 0;
      this.selectValue.forEach((i) => {
        p += i.price * i.store_nums;
      });
      return p;
    },
  },
  watch: {},
};
</script>

<style lang="scss" scoped></style>
